import Vue from "vue";
import NProgress from "nprogress";
import VueRouter from "vue-router";
import routerIn from "./routerIn";
Vue.use(VueRouter);
NProgress.inc(0.2);
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: true });
const router = new VueRouter({
  mode: 'history',
  base: '/wpsmt',
  routes: routerIn
})

// 路由拦截
router.beforeEach((to, from, next) => {
  NProgress.start();
  /* 路由发生变化修改页面meta */
  if (to.meta.content) {
    let head = document.getElementsByTagName('head');
    let meta = document.createElement('meta');
    meta.content = to.meta.content;
    head[0].appendChild(meta)
  }
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    let accessToken = window.localStorage.getItem('wpsmAccessToken');
    let refreshToken = window.localStorage.getItem('wpsmRefreshToken');
    if (accessToken && refreshToken) {
      next();
    } else {
      next({ path: '/login', query: { redirect: to.name } })
    }
  } else {
    next();
  }
});
router.afterEach(() => {
  NProgress.done()
});
export default router
